import { Component, OnInit, Input, Output, EventEmitter, SimpleChanges, OnChanges } from '@angular/core';
import { Farm } from "../../../entity/farm.model";
import { ApplicationService } from "../../../service/application.service";
import { FarmCategoryEnum } from '../../common/device-tag-selector/farm-category.enum';

@Component({
  selector: 'app-farm-selector',
  templateUrl: './farm-selector.component.html',
  styleUrls: ['./farm-selector.component.css']
})
export class FarmSelectorComponent implements OnInit, OnChanges {
  /**
   * 风场集合组件
   * 
   * 
   * @memberof FarmSelectorComponent
   */
  @Output() currentFarmChange: EventEmitter<Farm> = new EventEmitter<Farm>();

  /**
 * 电站类别
 * 默认:FarmCategoryEnum.winfarm 风场
 * @type {FarmCategory}
 * @memberof DeviceSelectorComponent
 */
  @Input() farmCategory: FarmCategoryEnum = FarmCategoryEnum.winfarm;
  /**
 * 当前风场id
 * 
 * @type {number}
 * @memberof FarmSelectorComponent
 */
  @Input() farmId: number = 0;

  /**
   * 风场集合
   * 
   * @type {Farm[]}
   * @memberof FarmSelectorComponent
   */
  farms: Farm[];

  /**
   * 当前风场
   * 
   * @type {Farm}
   * @memberof FarmSelectorComponent
   */
  currentFarm: Farm;
  /**
   * 标题
   * 
   * @memberof FarmSelectorComponent
   */
  title = '风场';


  constructor(
    public application: ApplicationService,
  ) {
    let farmCategoryCode = '';
    switch (this.farmCategory) {
      case FarmCategoryEnum.winfarm:
        farmCategoryCode = 'winfarm';
        this.title = '风场';
        break;
      case FarmCategoryEnum.photovoltaic:
        farmCategoryCode = 'photovoltaic';
        this.title = '光伏站';
        break;
    }
    this.farms = this.application.currentUserFarms.filter(farm => farm.farmCategory.startsWith(farmCategoryCode));
  }
  ngOnInit() {
  }
  ngOnChanges(changes: SimpleChanges) {
    if (changes['farmCategory'] && changes['farmCategory'].currentValue) {
      let farmCategoryCode = '';
      switch (this.farmCategory) {
        case FarmCategoryEnum.winfarm:
          farmCategoryCode = 'winfarm';
          this.title = '风场';
          break;
        case FarmCategoryEnum.photovoltaic:
          farmCategoryCode = 'photovoltaic';
          this.title = '光伏站';
          break;
      }
      this.farms = this.application.currentUserFarms.filter(farm => farm.farmCategory.startsWith(farmCategoryCode));
    }
    if (changes['farmId'] !== null && changes['farmId'].currentValue !== null) {
      this.getCurrentFarm();
    }
  }
  getCurrentFarm() {
    if (this.farmId !== null) {
      const index = this.farms.findIndex(o => o.farmId == this.farmId);
      if (index > -1) {
        this.currentFarm = this.farms[index];
        this.currentFarmChange.emit(this.currentFarm);
        return false;
      }
    }
    this.currentFarm = this.farms[0];
    this.currentFarmChange.emit(this.currentFarm);
  }
  public onClick(farm: Farm) {
    this.currentFarm = farm;
    this.currentFarmChange.emit(this.currentFarm);
  }

}



// WEBPACK FOOTER //
// ./src/app/module/monitor/farm-selector/farm-selector.component.ts